package com.elysia.newsservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elysia.newsservice.domain.dto.CarouselDTO;
import com.elysia.newsservice.domain.entity.Carousel;
import com.elysia.newsservice.domain.vo.CarouselVO;
import com.elysia.newsservice.feign.FileFeignClient;
import com.elysia.newsservice.mapper.CarouselMapper;
import com.elysia.newsservice.result.Result;
import com.elysia.newsservice.service.ICarouselService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图服务实现类
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ICarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements ICarouselService {
    private final FileFeignClient fileFeignClient;
    /**
     * 添加轮播图
     * @param carouselDTO 添加的轮播图信息
     * @return 添加成功后的轮播图信息
     */
    @Override
    public Result<CarouselVO> addCarousel(CarouselDTO carouselDTO) {
        Carousel carousel = Carousel.builder()
                .title(carouselDTO.getTitle())
                .fileKey(carouselDTO.getFileKey())
                .description(carouselDTO.getDescription())
                .build();
        save(carousel);
        return Result.success(CarouselVO.builder()
                .id(carousel.getId())
                .title(carousel.getTitle())
                .image(fileFeignClient.getFileUrl(carousel.getFileKey(), 3600))
                .description(carousel.getDescription())
                .build());
    }
    /**
     * 获取所有轮播图
     */
    @Override
    public Result<List<CarouselVO>> getAllCarousel() {
        List<CarouselVO> result = new ArrayList<>();
        List<Carousel> carouselList = list();
        for (Carousel carousel : carouselList) {
            result.add(CarouselVO.builder()
                    .id(carousel.getId())
                    .title(carousel.getTitle())
                    .image(fileFeignClient.getFileUrl(carousel.getFileKey(), 3600))
                    .description(carousel.getDescription())
                    .build());
        }
        return Result.success(result);
    }
    /**
     * 删除轮播图
     * @param id 轮播图id
     * @return 删除结果
     */
    @Override
    public Result<String> deleteCarousel(Long id) {
        String imageKey = lambdaQuery().eq(Carousel::getId, id).one().getFileKey();
        removeById(id);
        fileFeignClient.deleteFile(imageKey);
        return Result.success();
    }
    /**
     * 修改轮播图
     * @param carouselDTO 修改的轮播图信息
     * @return 修改后的轮播图信息
     */
    @Override
    public Result<CarouselVO> updateCarousel(Long id, CarouselDTO carouselDTO) {
        Carousel carousel = lambdaQuery().eq(Carousel::getId, id).one();
        carousel.setTitle(carouselDTO.getTitle());
        carousel.setDescription(carouselDTO.getDescription());
        if (carouselDTO.getFileKey() == null)
            carousel.setFileKey(carousel.getFileKey());
        else {
            fileFeignClient.deleteFile(carousel.getFileKey());
            carousel.setFileKey(carouselDTO.getFileKey());
        }
        updateById(carousel);
        return Result.success(CarouselVO.builder()
                .id(carousel.getId())
                .title(carousel.getTitle())
                .image(fileFeignClient.getFileUrl(carousel.getFileKey(), 3600))
                .description(carousel.getDescription())
                .build());
    }
}
